package eu.jba.denshijisho;

import java.util.Date;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import eu.jba.denshijisho.dao.CardDao;
import eu.jba.denshijisho.dao.StatDao;
import eu.jba.denshijisho.entity.Card;
import eu.jba.denshijisho.entity.Stat;
import eu.jba.denshijisho.exceptionhandler.ExceptionHandler;
import eu.jba.denshijisho.util.Constants;
import eu.jba.denshijisho.util.Util;

public class ViewStatActivity extends AbstractDenshiActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Thread.setDefaultUncaughtExceptionHandler( new ExceptionHandler( this ));
		setContentView(R.layout.activity_view_stat);
		setFinishOnTouchOutside(false);
		final Bundle bundle = getIntent().getExtras();
		final int cardId = bundle.getInt(Constants.CARD_ID);
		final Card card =  getCard( cardId );
		card.setStat( getStat( card.getStatId() ));
		((TextView)findViewById(R.id.slovensky_output_stat)).setText( card.getSvkWord() );
		((TextView)findViewById(R.id.kana_output_stat)).setText( card.getKanaWord() );
		((TextView)findViewById(R.id.kanji_output_stat)).setText( card.getKanji() );
		((TextView)findViewById(R.id.next_date_edit_stat)).setText(Util.dateToString(card.getStat().getNextDate()));
		((TextView)findViewById(R.id.previous_date_edit_stat)).setText(Util.dateToString(card.getStat().getPreviousDate()));
		((TextView)findViewById(R.id.right_answer_value)).setText(card.getStat().getOkAnswers().toString());
		((TextView)findViewById(R.id.wrong_answer_value)).setText(card.getStat().getWrongAnswers().toString());
		((TextView)findViewById(R.id.total_value)).setText(card.getStat().getReps().toString());
		((TextView)findViewById(R.id.interval_value)).setText(card.getStat().getInterval().toString());

	}
	
	private Card getCard(int id){
		final CardDao cardDao = new CardDao( this );
		cardDao.open();
		final Card card = cardDao.get( id );
		cardDao.close();
		return card;
	}
	
	private Stat getStat(int id){
		final StatDao statDao = new StatDao( this );
		statDao.open();
		final Stat stat = statDao.get( id );
		statDao.close();
		return stat;
	}
	
	public void closeDialog(View view){
		setResult(RESULT_CANCELED);
		finish();		
	}
	
	public void resetStats(View view){
		new AlertDialog.Builder(this).
		setMessage("Do you really want to reset statisticsS?").setCancelable( false ).
		setPositiveButton("Yes", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				final Bundle bundle = getIntent().getExtras();
				final Card card = (Card)bundle.getSerializable(Constants.CARD);

				resetStat( card.getStat() );
				
				final StatDao statDao = new StatDao(ViewStatActivity.this);
				statDao.open();
				statDao.update(card.getStat());
				statDao.close();
				
				setResult(RESULT_OK);
				finish();								
			}
		}).setNegativeButton("No", null).show();
	}
	
	private void resetStat(Stat stat){
		stat.setEf(2.5);
		stat.setInterval(0);
		stat.setNextDate( new Date() );
		stat.setPreviousDate(new Date() );
		stat.setOkAnswers(0);
		stat.setReps(0);
		stat.setWrongAnswers(0);
	}
}
